<?php
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
class member { 
    function __construct() {
        $this->categorys = getcache('category','commons');
        $this->models = getcache('model','commons');
        $this->convert_db = D('Convert');
        $this->detail = D('Member_detail');
        $this->db = D('Member');
        $this->cash = D('Cash');
        $this->bean = D('Bean_info');
        $this->plat = D('Games');
        $this->dama = D('Dama');
        $this->damalog = D('DamaLog');
        $this->dama_account = D('DamaAccount');
        $this->score = D('Score');
    }

    public function lists($data) {
    	$sqlMap = array();
    	if (isset($data['where'])) {
    		$sqlMap['_string'] = $data;
    	}
    	if (isset($data['uids'])) {
    		$uids = (!is_array($data['uids'])) ? explode(",", $data['uids']) : $data['uids'];
    		$sqlMap['userid'] = array("IN", $uids);
    	}
    	return D('Member')->where($sqlMap)->order($data['order'])->limit($data['limit'])->select();
    }

    public function activitie($data) {
        
    }

    /**
     * 大家都在玩
     */
    public function playlist($data){
        
        
    }



    /**
     * 大家都在兑
     */
    public function convertlist($data){
        $order = (isset($data['order']) && !empty($data['order'])) ? $data['order'] : 'id DESC';
        $tmps = $this->convert_db->where(array('status'=>'1'))->limit($data['limit'])->order($order)->select();
        foreach ($tmps as $key => $v) {
            $v['date'] = time() - $v['time'];
            $v['photo'] = $this->detail->where(array('userid'=>$v['userid']))->getField('photo');

            if(ceil($tmps['date']/3600) > 0 ){
                $v['date'] = ceil($tmps['date']/3600);                
            }
            $info[] = $v;          
        }
        return $info;
    }
    /**
     * 提现记录
     */
    public function cashList($data){
        $page = I('page', '0', 'int');
        $count = $this->cash->where(array('userid'=>$data['id']))->count();

        $cash_db = $this->cash->where(array('userid'=>$data['id']))->order("cash_id DESC")->page($page, 5)->select();
       
        return $cash_db;    
    } 
    /**
     * 打码记录
     */
    public function datalog($data){
        $page = I('page', '0', 'int');
        $result = $this->damalog->where(array('uid'=>$data['id']))->order("id DESC")->page($page, 5)->select();

        $tmps = array();
        foreach ($result as $key => $v) {
           $tmps[] = $v['aid'];
        }
        
        $account = array();
        $account['id'] = array('IN',$tmps);
        $dama = $this->dama->where($account)->getField('id,title,updatetime',true);

        for($i=0;$i<count($result);$i++){
            foreach ($dama as $key => $value) {
                if($result[$i]['aid'] == $key){
                    $result[$i]['title'] = $value['title'];
                    $result[$i]['updatetime'] = $value['updatetime'];
                }
            }
        }

        $dama_account = $this->dama_account->where(array('uid'=>$data['id']))->getField('name',true);
        $dama_account = array_unique($dama_account);
        $name = $dama_account[0];
        for($i=0;$i<count($result);$i++){
           $result[$i]['name'] = $name;
        }
        $pages = page($count,5);
        return $result;


    }
    
    /**
     * 会员中心推荐收入榜
     */
    public function reincome($data){
        $start = strtotime(date('Y-m-01 00:00:00',time()));
        $end = strtotime(date('Y-m-t 23:59:59',time()));

        $sql_map = "posttime > ".$start." AND ".$end." > posttime AND c_type =1 AND status = '1' AND u_type = 'rec'";

        $infos = $this->score->where($sql_map)->limit(0,10)->order('`change` DESC')->select();
        $user = array();
        foreach ($infos as $key => $v) {
            $user[] = $v['userid'];
        }
        $user = array_unique($user);
        $tmp = array();
        $tmp['c_type'] = '1';
        $tmp['status'] = '1';
        $tmp['u_type'] = 'rec';

        $tmp['userid'] = array("IN",$user);
        $infos = $this->score->field("userid,SUM(`change`)")->where($tmp)->limit(0,10)->order('`change` DESC')->group('userid')->select();
        $users['userid'] = array("IN",$user);
        $user_info = $this->db->where($users)->getField('userid,nickname');
        for($i=0;$i<count($infos);$i++){
            foreach ($user_info as $k => $t) {
                if($k == $infos[$i]['userid'])
                    $infos[$i]['nickname'] = $t;
                    $infos[$i]['change'] = $infos[$i]['SUM(`change`)'];
            }
        }
        return $infos;
    }
}